package org.ttitfly.ttcore.service.account;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.ttitfly.ttcore.dao.account.RoleDao;
import org.ttitfly.ttcore.dao.menu.SystemMenuDao;
import org.ttitfly.ttcore.entity.account.Role;
import org.ttitfly.ttcore.so.account.RoleSO;

//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class RoleManager {

	private static Logger logger = LoggerFactory.getLogger(RoleManager.class);

	@Resource
	private RoleDao roleDao;
	
	@Autowired
	private SystemMenuDao systemMenuDao;

	//-- Role Manager --//
	@Transactional(readOnly = true)
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

	public void saveRole(Role entity) {
		roleDao.save(entity);
	}
	
	public void updateRole(Role entity) {
		roleDao.save(entity);
	}

	/**
	 * 删除用户,如果尝试删除超级管理员将抛出异常.
	 */
	public void deleteRole(Long id) {
		roleDao.delete(id);
	}
	public void deleteRole(Role role) {
		roleDao.delete(role);
	}

	/**
	 * 使用属性过滤条件查询用户.
	 */
	@Transactional(readOnly = true)
	public Page<Role> searchRole(final Page<Role> page, final List<PropertyFilter> filters) {
		return roleDao.findPage(page, filters);
	}

	@Transactional(readOnly = true)
	public Role findRoleByName(String name) {
		return roleDao.findUniqueBy("name", name);
	}

	
	public int getSORoleViewCount(RoleSO so) {
        return roleDao.getSORoleViewCount(so);
    }

	public List<Role> getSORoleView(RoleSO so) {
        return roleDao.getSORoleView(so);
    }
	
	public List<Role> getAllRoles(){
		return roleDao.getAll();
	}
	
	public List<Role> getByIds(List<Long> ids){
		if (ids != null && ids.size()>0) {
            return roleDao.getByIds(ids);
        } else {
            return new ArrayList<Role>(0);
        }
	}

	
}
